const util = require('../../utils/util.js')
const Monitor = require('../../utils/monitor.js').Monitor
const mr = getApp().globalData.imgUrl + 'jsy-newshop星星复制 6.svg';
const xz = getApp().globalData.imgUrl + 'jsy-newshop星星复制 5.svg';

Page(Monitor.hookApp({
  data: {
    imgUrl: getApp().globalData.imgUrl,
    imgUrls: getApp().globalData.imgUrls,
    stars: [{
        flag: 2,
        mr,
        xz
      },
      {
        flag: 2,
        mr,
        xz
      },
      {
        flag: 2,
        mr,
        xz
      },
      {
        flag: 2,
        mr,
        xz
      },
      {
        flag: 2,
        mr,
        xz
      }
    ],
    star: [],
    content: [],
    idx: '', // 点击的星星的个数
    clickd: false,
    textareaVal: ''
  },
  onLoad(o) {
    console.log(o)
    this.getImg(o.order_id)
  },
  getImg(order_id) {
    util.API('spwx/v1.shop/showOrderGoods', {
      order_id
    }).then(res => {
      let {
        code,
        data,
        msg
      } = res
      if (code == 200) {
        data.map((item, index) => {
          item.content = ''
          item.clickd = false
          item.textareaVal = ''
          item.stars = this.data.stars
          item.dataLeng = 4
        })
        this.setData({
          data: data
        })
      }
    })
  },
  //星星评价
  click(e) {
    let idx = e.currentTarget.dataset.idx
    let indexs = e.currentTarget.dataset.indexs
    for (var i = 0; i < this.data.data[indexs].stars.length; i++) {
      if (i <= idx) {
        var data = 'data[' + indexs + '].stars[' + i + '].flag'
        var dataLeng = 'data[' + indexs + '].dataLeng'
        this.setData({
          [data]: 2,
          [dataLeng]: idx + 1
        })
      } else {
        var data = 'data[' + indexs + '].stars[' + i + '].flag'
        this.setData({
          [data]: 1
        })
      }
    }
  },
  //textarea显示隐藏
  textareaShow(e) {
    let data = this.data.data,
      idx = e.currentTarget.dataset.idx;
    data[idx].clickd = true
    this.setData({
      data
    })
  },
  check(e) {
    let data = this.data.data,
      idx = e.currentTarget.dataset.idx;
    if (data[idx].content == '') {
      data[idx].clickd = false
    }
    this.setData({
      data
    })
  },
  //获取value
  getVal(e) {
    let idx = e.currentTarget.dataset.idx,
      val = e.detail.value;
    this.data.data[idx].content = val
    this.setData({
      data: this.data.data
    })
  },
  //提交
  submit() {
    console.log(this.data.data)
    delete this.data.data[0].create_time
    util.API('spwx/v1.Shop/comment', {
      json: JSON.stringify(this.data.data)
    }).then(res => {
      var {
        code,
        data,
        msg
      } = res
      if (code === 200) {
        wx.showToast({
          title: '晒单成功',
          success() {
            setTimeout(() => {
              let pages = getCurrentPages();
              wx.navigateBack({
                delta: pages.length + 1
              })
            }, 1500)
          }
        })
      }
    })
  }
}))